package com.bosssoft.hr.jobmonitor.controller;
import com.bosssoft.hr.jobmonitor.config.common.SysConfig;
import com.bosssoft.hr.jobmonitor.core.file.FileTool;
import com.bosssoft.hr.jobmonitor.entity.Auth;
import com.bosssoft.hr.jobmonitor.entity.User;
import com.bosssoft.hr.jobmonitor.service.IUserService;
import com.bosssoft.hr.jobmonitor.util.LoggerUtil;
import com.bosssoft.hr.jobmonitor.util.ResponseUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

/***
 * @Author lys(cherish)
 * @Description 权限校验
 * @Date  2019/2/26
 **/
@RestController
@RequestMapping("/login")
public class LoginController {

    private static final Logger logger= LoggerFactory.getLogger(LoginController.class);

    //用户服务注入
    @Resource(name = "xmlUserServiceImpl")
    private IUserService userService;

    //文件处理工具注入
    @Autowired
    private FileTool fileTool;



    /***
     * @Author lys(cherish)
     * @Description 管理员简易登入
     * @Date  2019/2/26
     * @Param [username, password, session] 用户名 密码 响应对象
     * @return java.lang.Object 响应对象
     **/
    @RequestMapping("/admin")
    public Object adminLogin(@RequestParam String username, @RequestParam String password, HttpSession session){
        logger.info(LoggerUtil.fmtLog("进入管理员校验"));
        SysConfig sysconfig = fileTool.getSysConfig();
        if(sysconfig.getAuth()==null){
            return ResponseUtil.badAuthConfig();
        }
        ModelAndView modelAndView = new ModelAndView();
        Auth auth = sysconfig.getAuth();
        if(!username.isEmpty()&&!password.isEmpty()){
            if(username.equals(auth.getUsername())&&password.equals(auth.getPassword())){
                logger.info(LoggerUtil.fmtLog("管理员校验通过",auth));
                session.setAttribute("auth",auth);
                modelAndView.setViewName("redirect:/user/list");
                return modelAndView;
            }else {
                modelAndView.setViewName("login");
                return modelAndView;
            }
        }
        return ResponseUtil.badParam();
    }

    /***
     * @Author lys(cherish)
     * @Description 用户简易登入
     * @Date  2019/2/26
     * @Param [username, password]用户名 密码
     * @return java.lang.Object 响应对象
     **/
    @RequestMapping("/user")
    public Object userLogin(@RequestParam String username,@RequestParam String password){
        if(!username.isEmpty()&&!password.isEmpty()){
            if(username.equals("user")&&password.equals("123")){
                logger.info(LoggerUtil.fmtLog("用户校验通过",username,password));
                return ResponseUtil.successful();
            }else {
                return ResponseUtil.fail();
            }
        }
        return ResponseUtil.badParam();
    }

}
